<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="../include/tag.jsp"%>
<jsp:useBean id="now" class="java.util.Date" scope="page"/>
<!DOCTYPE html>
<html lang="zh">
<html>
<head>
    <title>短信推送</title>
    <%@include file="../include/commonFile.jsp"%>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
    <style type="text/css">
        .index-outside {
            min-height: auto !important;
        }

        .index-outside > section .section-main {
            min-width: auto !important;
        }
    </style>
</head>
<body>

<div class="index-outside">
    <section class="no-fix">
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <input type="hidden" id="types" value="${types}"/>
            <div class="add-form-content">
            <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/crowdfund/target/saveForCrowdfund.do">
                <input type="hidden" name="targetId" value="${targetTemplate.targetId}" />
                <input type="hidden" id="id" name="id" value="${targetTemplate.id}"/>
                <input type="hidden" id="behavior" name="behavior" value="${behavior}"/>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">发送对象<span class="f-verify-red">*</span></label>
                        <div class="layui-input-inline">
                            <select name="type" id="type" lay-verify="type" lay-filter="type" <c:if test="${behavior != 'add'}">disabled="disabled"</c:if>>
                                <c:forEach var="type" items="${types}">
                                    <option value="${type.key}" ${type.key == targetTemplate.type ? 'selected="selected"' : ""}>${type.value}</option>
                                </c:forEach>
                            </select>
                        </div>
                        <c:if test="${packageCount.isOpen == 1}">
                            <div class="layui-form-mid layui-word-aux">当前短信剩余<span class="red">${packageCount.totalNum - packageCount.useNum}</span>条</div>
                            <div class="layui-form-mid layui-word-aux"><a href="javascript:forwardCharge();" style="color: #1E9FFF">充值</a></div>
                        </c:if>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">发送时间<c:if test="${behavior != 'add'}"><span class="f-verify-red">*</span></c:if></label>
                        <div class="layui-input-inline">
                            <input class="layui-input" type="text" name="sendTimeString"
                                   value="<fmt:formatDate value="${now}" pattern="yyyy-MM-dd HH:mm:ss" />" placeholder="发送时间">
                            <c:if test="${behavior != 'modifyTime'}">
                                <div class="layui-form-mid layui-word-aux" style="width: 400px">发送时间小于或等于当前时间,则会立即发送</div>
                            </c:if>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">模板配置<span class="f-verify-red">*</span></label>
                    <div class="layui-input-block">
                      <textarea name="template" placeholder="模板配置" lay-verify="template" class="layui-textarea" style="width: 85%"
                                maxlength="200" onchange="this.value=this.value.substring(0, 300)"
                                onkeydown="this.value=this.value.substring(0, 300)"
                                onkeyup="this.value=this.value.substring(0, 300)"
                                <c:if test="${behavior != 'add'}">disabled="disabled"</c:if>>${targetTemplate.template}</textarea>
                        <div class="layui-form-mid layui-word-aux">最多200字符</div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">说明：</label>
                    <p class="pt10">{user} 表示众筹者, {project} 表示众筹名称, {favorerNum} 表示支持人数，{actualAmount} 表示筹集资金</p>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">模板：</label>
                    <span class="content">{user} ，还有1天〔创投戈壁行〕众筹将截止，众勇士将在5天后敦煌集结。走在荒无人烟的茫茫戈壁上，你会为自己而感到，而骄傲，甚至自豪！</span>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" style="height: 30px">内容：</label>
                    <span class="content">王小雪 ，还有1天〔创投戈壁行〕众筹将截止，众勇士将在5天后敦煌集结。走在荒无人烟的茫茫戈壁上，你会为自己而感到，而骄傲，甚至自豪！</span>
                </div>
            </form>
            </div>
        </div>
    </section>
</div>
<!--底部-->
<script type="text/javascript" src="${ctx}/static/layui-v2.2.45/layui/layui.all.js"></script>
<script type="text/javascript">
    $(function () {
        layui.use([ 'form', 'laydate' ], function() {
            var form = layui.form, laydate = layui.laydate;
            form.on('select(type)', function(data) {
                console.log(data);
                window.location.href = "${ctx}/crowdfund/target/messageView.do?targetId=${targetTemplate.targetId}&type="
                    + data.value + '&behavior=' + $('#behavior').val() + '&id=' + $('#id').val();
            });

            var sendTime = {
                elem: 'input[name=sendTimeString]',
                type: 'datetime'
            };
            laydate.render(sendTime);
        });
    })

    function doSubmit() {
        var result = true;

        if (!checkPersonNum()) {
            util.layerMsgError("请选择人数不为零的发送对象")
            return false;
        }

        var template = $('textarea[name=template]').val();
        if (!template || !template.trim()) {
            util.layerMsgError("请填写模板配置")
            result = false;
            return result;
        }
        $.ajax({
            type: 'POST',
            async: false, // 使用同步的方法
            data: $('#myForm').serialize(),
            dataType: 'json',
            success: function (res) {
                if (res.success) {
                    util.layerMsgSuccess("发送成功")
                } else {
                    util.layerMsgError(res.description)
                    result = false;
                }
            },
            url: $("#myForm").attr('action')
        });
        return result;
    }

    function submitQuartz() {
        var result = true;

        /*if (!checkPersonNum()) {
            top.layer.msg('请选择人数不为零的发送对象', {icon: 2});
            return false;
        }*/

        var template = $('textarea[name=template]').val();
        if (!template || !template.trim()) {
            util.layerMsgError("请填写模板配置")
            result = false;
            return result;
        }
        var sendTimeString = $('input[name=sendTimeString]').val();
        if (!sendTimeString) {
            util.layerMsgError("请设置发送时间")
            result = false;
            return result;
        }
        if ($('#behavior').val() == 'modifyTime') {
            var now = new Date();
            if (now.getTime() > new Date(sendTimeString).getTime()) {
                util.layerMsgError("发送时间必须大于当前时间!")
                result = false;
                return result;
            }
        }
        $.ajax({
            type: 'POST',
            async: false, // 使用同步的方法
            data: $('#myForm').serialize(),
            dataType: 'json',
            success: function (res) {
                if (res.success) {
                    util.layerMsgSuccess("发送成功")
                } else {
                    util.layerMsgError(res.description)
                    result = false;
                }
            },
            url: '${ctx}/crowdfund/target/sendQuartz.do'
        });
        return result;
    }

    function forwardCharge() {
        parent.location.href = "${ctx}/charge/package/smsPackageList.do";
    }

    function checkPersonNum() {
        var types = $("#types").val();
        var index = $('#type option:selected').val();
        var typeStr = findValue(index, types);
        var reg = /([^\d]+)(\d+)([^\d]+)/;
        var r = typeStr.match(reg);
        if (!r) {
            return false;
        }
        var num = typeStr.replace(reg, '$2');
        if (num == 0) {
            return false;
        } else {
            return true;
        }
    }

    function findValue(key, str) {
        var index = str.indexOf(key + "=");
        var key_new = key + "=";
        var sub = str.substring(index + key_new.length);

        var key_last = '';
        if (sub.indexOf(",") > -1) {
            key_last = ",";
        } else {
            key_last = "}";
        }
        sub = sub.substring(0, sub.indexOf(key_last));
        return sub;
    }
</script>
</body>
</html>
